var login = require("login.js");
var common=require('common.js');
const HTTPManager={};
HTTPManager.post = function (url, parm) {
      parm.token = wx.getStorageSync('token') || '';
      //没有token的情况直接重新登录获取token
      if(!parm.token){
        return new Promise(function(success,fail){
          login.login('logout').then(function (res) {
            //console.log('重新登录校验token')
            if (res.code == 0) {
              res.token&&wx.setStorageSync('token', res.token)
              parm.token = res.token;
              console.log(url+':'+JSON.stringify(parm))
              const rsaData=common.getRsa(parm),time=new Date().getTime(), Authorization=common.getAuthorization(rsaData,time),systeminfo=login.getSystemInfo();
              wx.request({
                url: url,
                data: rsaData,
                method: "POST",
                header: {
                  'content-type': 'application/json',
                  'From':'xqx_c_wxxcx',
                  'Authorization':Authorization,
                  'DateTime':time,
                  'TerminalEnv':systeminfo
                },
                success: function (res) {
                  //console.log(res.header)
                  success&&success(res.data);
                },
                fail:function(err){
                  fail&&fail(err)
                }
              })
            } else {
              wx.showToast({
                title: '网络错误请稍后再试',
                icon: 'none',
                duration: 2000,
              });
            }
          })
        })
      }else{
        const rsaData=common.getRsa(parm),time=new Date().getTime(), Authorization=common.getAuthorization(rsaData,time),systeminfo=login.getSystemInfo();
        console.log(url+':'+JSON.stringify(parm))
        return new Promise(function (success, fail) {
          wx.request({
            url: url,
            data: rsaData,
            method: "POST",
            header: {
              'content-type': 'application/json',
              'From':'xqx_c_wxxcx',
              'Authorization':Authorization,
              'DateTime':time,
              'TerminalEnv':systeminfo
            },
            success: function (res) {
              // console.log(res);
              // if (res.data.code == 2 ) {
              //   console.warn('其他接口验证需要跳转手机号页面-调用登录接口再次验证')//现已废弃
              //   login.login().then(function (res) {
              //     console.log('重新登录校验token')
              //     console.log(res)
              //     if (res.code == 0) {
              //         res.token&&wx.setStorageSync('token', res.token)
              //         parm.token = res.token;
              //         const rsaData=common.getRsa(parm),time=new Date().getTime(),Authorization=common.getAuthorization(rsaData,time)
              //         console.log(url+':'+JSON.stringify(parm))
              //         wx.request({
              //           url: url,
              //           data: rsaData,
              //           method: "POST",
              //           header: {
              //             'content-type': 'application/json',
              //             'From':'xqx_c_wxxcx',
              //             'Authorization':Authorization,
              //             'DateTime':time,
              //             'TerminalEnv':systeminfo
              //           },
              //           success: function (res) {
              //             success&&success(res.data);
              //           },
              //           fail:function(err){
              //             fail&&fail(err)
              //           }
              //         })
              //     } else {
              //       wx.showToast({
              //         title: '网络错误请稍后再试',
              //         icon: 'none',
              //         duration: 5000,
              //       });
              //     }
              //   })
              //   return;
              // }
              success(res.data);
            },
            fail: function (error) {
              fail(error)
            }
          })
        });
      }
}
HTTPManager.post1 = function (url, parm) {
    const rsaData=common.getRsa(parm),time=new Date().getTime(),Authorization=common.getAuthorization(rsaData,time),systeminfo=login.getSystemInfo();
    console.log(url+':'+JSON.stringify(parm))
    return new Promise(function (success, fail) {
      wx.request({
        url: url,
        data: rsaData,
        method: "POST",
        header: {
          'content-type': 'application/json',
          'From':'xqx_c_wxxcx',
          'Authorization':Authorization,
          'DateTime':time,
          'TerminalEnv':systeminfo
        },
        success: function (res) {
          success&&success(res.data);
        },
        fail: function (error) {
          fail&&fail(error)
        }
      })
    });
}
module.exports = HTTPManager ;
